package com.jx.lcs.datareport.service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jx.lcs.datareport.dao.UserDeptReportDao;
import com.jx.lcs.datareport.entity.DataReport;
import com.jx.lcs.util.export.Excel4MapExporter;
import com.jx.lcs.util.export.ExcelExporter;
import com.jx.lcs.util.export.SheetDefination;
import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.service.BaseService;

@Service
@Transactional(readOnly = true)
public class UserDeptReportService extends BaseService<DataReport, String> {

  @Autowired
  private UserDeptReportDao userDeptReportDao;

  @Override
  public HibernateDao<DataReport, String> getEntityDao() {
    return userDeptReportDao;
  }

  /**
   * 根据部门统计B端用户
   */
  public List<Map<String, Object>> countUserByDept() {
    return userDeptReportDao.countUserByDept();
  }
  
  /**
   * 导出数据
   */
  public void exportDataReport(OutputStream out) throws IOException{
    SheetDefination<Map<String, Object>> sheetDefination = new SheetDefination<>();
    sheetDefination.setSheetTitle("各营业部理财师用户量");
    sheetDefination.setHeaders(new String[] {"营业部", "用户数量"});
    sheetDefination.setDataKeys(new String[] {"userDept", "totall"});
    sheetDefination.setDataset(this.countUserByDept());
    
    ExcelExporter<Map<String, Object>> exportExcel = new Excel4MapExporter();
    exportExcel.export(sheetDefination, out);
  }
}
